<%#
 Copyright 2013-2018 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://jhipster.github.io/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%=packageName%>.security.oauth2;

import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.http.AccessTokenRequiredException;
import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class AuthorizationHeaderUtil {

    private final Logger log = LoggerFactory.getLogger(getClass());

    private final OAuth2RestTemplate oAuth2RestTemplate;

    public AuthorizationHeaderUtil(OAuth2RestTemplate oAuth2RestTemplate) {
		this.oAuth2RestTemplate = oAuth2RestTemplate;
	}

    public Optional<String> getAuthorizationHeaderFromOAuth2Context() {
        OAuth2AccessToken previousAccessToken = oAuth2RestTemplate.getOAuth2ClientContext().getAccessToken();
        if (previousAccessToken == null) {
            return Optional.empty();
        } else {
            OAuth2AccessToken accessToken;
            try {
                // Get the token from OAuth2ClientContext and refresh it if necessary
                accessToken = oAuth2RestTemplate.getAccessToken();
            } catch (UserRedirectRequiredException e) {
                // It's a refresh failure (because previous token wasn't null)
                // If it's an AJAX Request, this sends a 401 error
                throw new AccessTokenRequiredException("Refreshing access token failed",null);
            }

            String tokenType = accessToken.getTokenType();
            if (!StringUtils.hasText(tokenType)) {
                tokenType = OAuth2AccessToken.BEARER_TYPE;
            }
            String authorizationHeaderValue = String.format("%s %s", tokenType, accessToken.getValue());
            return Optional.of(authorizationHeaderValue);
        }
    }
}
